<?php

class Content_Model_DbTable_Articles extends Zend_Db_Table_Abstract {

    protected $_name = 'content_articles';

    public function getList($categoryId = null) {
        $categories = new Content_Model_DbTable_Categories();

        $select = $this->getAdapter()->select()
                ->from(array('a' => $this->_name), array('id', 'title'))
                ->columns(array(
                    'text' => new Zend_Db_Expr('LEFT(a._text, 100)'),
                ))
                ->joinLeft(array('c' => $categories->info('name')), 'a.category = c.id', array('category' => 'title'))
        ;

        if ($categoryId) {
            $select->where('category = ?', $categoryId);
        }
//        Zend_Debug::dump($select->__toString());
//        exit;

        return $this->getAdapter()->fetchAll($select);
    }

    public function findShort($id) {
        $select = $this->select()
                ->from($this->_name, array('id', 'title', 'category', '_text'))
                ->where('id = ?', $id)
        ;
        return $this->fetchRow($select);
    }

    public function findListByCategory($category) {
        $id = is_integer($category) ? $category : $category->id;
        $select = $this->select()
                ->where('category = ?', $id)
        ;
        return $this->fetchAll($select);
    }

    public function findSingleByCategory($category) {
        $id = is_integer($category) ? $category : $category->id;
        $select = $this->select()
                ->where('category = ?', $id)
        ;
        return $this->fetchRow($select);
    }
    
}

